package top.atcra.rpc.config;

import lombok.Getter;
import lombok.Setter;
import top.atcra.rpc.compress.CompressorType;
import top.atcra.rpc.protection.circuitBreaker.CircuitBreaker;
import top.atcra.rpc.protection.rateLimit.RateLimiter;
import top.atcra.rpc.protection.circuitBreaker.impl.ProportionCircuitBreaker;
import top.atcra.rpc.protection.rateLimit.impl.TokenBucketLimiter;
import top.atcra.rpc.protection.loadBalancer.LoadBalanceType;
import top.atcra.rpc.serialization.SerializerType;

/**
 * 应用程序的配置类（POJO），对应于yml文件中的内容
 *
 * @author atcra
 * @date 2025-3-22
 */
@Getter
@Setter
public class AppConfig {
    private String appName = "RPC";
    private String groupName = "default";
    private int port = 50080;
    private String registryUrl = "zookeeper://localhost:2181";
    private String subnetPrefix = "";

    private byte serializer = SerializerType.jdk.getId();
    private byte compressor = CompressorType.none.getId();
    private byte loadBalancer = LoadBalanceType.round_robin.getId();

    private int dataCenterId = 0;
    private int clientId = 0;
    private int maxWaitTimeMs = 5000;

    private int heartbeatIntervalMs = 5000;

    private ZookeeperConfig zookeeper;
    private CircuitBreakerConfig circuitBreaker;

    // 以下属性没有暴露到配置文件中
    private int HealthCheckInterval_MS = 60000;
    private int HealthCheckTimeout_MS = 1000;
    private int SHUTDOWN_TIMEOUT_MS = 10000; // 服务停机时，等待未完成的请求处理时间

    public RateLimiter getRateLimiter() {
        return new TokenBucketLimiter(1000000, 100000);
    }

    public CircuitBreaker getCircuitBreaker() {
        return new ProportionCircuitBreaker(circuitBreaker.getError_rate_threshold(),circuitBreaker.getPeriod_ms());
    }
}

